#include <iostream>
#include <string>
// 包含SpireMS SDK头文件
#include <sms_core.h>


const char* about = "32 Nodes Stress Test with Share Memory";
const char* keys  =
    "{@node_id   |<none> | Node ID }"
    "{d          |/home/jario/dataset/YOLO-Box_2024-12-24_VisDrone2019-trainval-test/images/trainval   | Image Dir }";


int main(int argc, char *argv[])
{
    cv::CommandLineParser parser(argc, argv, keys);
    parser.about(about);

    if(argc < 2) {
        parser.printMessage();
        return 0;
    }

    std::cout << "node_id: " << parser.get<int>(0) << std::endl;
    std::cout << "img_dir: " << parser.get<std::string>("d") << std::endl;

    sms::mem_init();
    sms::Publisher pub("/share_mem/image_" + std::to_string(parser.get<int>(0)), "memory_msgs::RawImage");
    
    std::vector<std::string> im_names;
    sms::_list_dir(parser.get<std::string>("d"), im_names, ".jpg");
    for (std::string name : im_names)
    {
        ;  // std::cout << name << std::endl;
    }
    
    std::srand(std::time(0));
    int random_number = std::rand() % im_names.size();
    std::cout << random_number << std::endl;
    std::cout << im_names[random_number] << std::endl;
    cv::Mat img_o = cv::imread(im_names[random_number]);
    
    sms::Rate rate(25);

    int i=0;
    while (1)
    {
        cv::Mat img = img_o.clone();
        cv::resize(img, img, cv::Size(1920, 1080));
        auto now = std::chrono::system_clock::now();
        auto now_time_t = std::chrono::system_clock::to_time_t(now);
        auto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;

        // 将时间格式化为字符串
        std::tm tm = *std::localtime(&now_time_t);
        std::ostringstream oss;
        oss << std::put_time(&tm, "%H:%M:%S") << "." << std::setfill('0') << std::setw(3) << now_ms.count();
        std::string time_str = oss.str();

        // 在图像上绘制时间字符串
        cv::putText(img, time_str, cv::Point(200, 550), cv::FONT_HERSHEY_SIMPLEX, 7, cv::Scalar(187, 71, 0), 14);
        
        
        nlohmann::json msg = pub.cvimg2sms_mem(img);
        pub.publish(msg);
        
        cv::imshow("img", img);
        cv::waitKey(3);
        
        rate.sleep();
        i++;
        /*
        if (i > 10)
        {
            break;
        }
        */
    }

    pub.kill();
    return 0;
}

